package com.xubeizhi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xubeizhi.common.PageBean;
import com.xubeizhi.common.Result;
import com.xubeizhi.dto.ArticleDto;
import com.xubeizhi.entity.Article;
import com.xubeizhi.mapper.ArticleMapper;
import com.xubeizhi.service.ArticleService;
import com.xubeizhi.utils.DataUtils;
import com.xubeizhi.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    /**
     * 新增文章
     * @param article
     * @return
     */
    @Override
    public Result createArticle(Article article) {
        BeanUtils.copyProperties(article,Article.class);
        article.setUpdateTime(DataUtils.getDateNow());
        article.setCreateTime(DataUtils.getDateNow());
        int rows = baseMapper.insert(article);
        return rows > 0 ? Result.success(0,"操作成功",null) : Result.error(1,"操作失败",null);
    }

    /**
     * 分页查询
     * @param pageNum 当前页码
     * @param pageSize 每页分数
     * @return
     */
    @Override
    public Result listArticleInfo(Integer pageNum, Integer pageSize) {
        List<Article> articles = baseMapper.selectCategoryPage(pageNum, pageSize);

        return Result.success(1,"操作成功",articles);
    }

    /**
     * 根据id查询文章详情
     * @param id
     * @return
     */
    @Override
    public Result getArticleById(Integer id) {
        Article article = baseMapper.selectById(id);
        if (article != null){
            return Result.success(0,"操作成功",article);
        }
        return Result.error(1,"操作失败",null);
    }

    /**
     * 更新文章
     * @param article
     * @return
     */
    @Override
    public Result updateArticle(Article article) {
        article.setUpdateTime(DataUtils.getDateNow());
        int rows = baseMapper.updateById(article);
        return rows > 0 ? Result.success(0,"操作成功",null) : Result.error(1,"操作失败",null);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @Override
    public Result deleteArticle(Integer id) {
        int rows = baseMapper.deleteById(id);
        return rows > 0 ? Result.success(0,"操作成功",null) : Result.error(1,"操作失败",null);
    }

    /**
     * 根据详情和状态搜索文章
     * @param content
     * @param status
     * @return
     */
    @Override
    public Result SearchArticle(String content, String status) {
        List<Article> articles = baseMapper.SearchArticle(content, status);
        return Result.success(0,"查询成功",articles);
    }

    @Override
    public Result<PageBean<ArticleDto>> listArticles(Integer pageNum, Integer pageSize, Integer categoryId, String status) {
        //1.创建pageBean对象
        PageBean<ArticleDto> pb = new PageBean<>();


        //2.开启分页查询PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //调用Mapper
//        Map<String,Object> map = ThreadLocalUtil.get();
//        Integer userId = (Integer) map.get("id");
        List<ArticleDto> leiou = articleMapper.listArticle(categoryId,status);
        

        Page<ArticleDto> p  = (Page<ArticleDto>) leiou;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());

        return Result.success(0,"查询成功",pb);
    }

    /**
     * 删除文章（只在前端页面不显示，数据库内仍然留存，改状态）
     * @param article
     * @return
     */
    @Override
    public Result deleteByArticle(Article article) {
        article.setUpdateTime(DataUtils.getDateNow());
        article.setStatus("3");
        int rows = baseMapper.updateById(article);
        return rows > 0 ? Result.success(0,"操作成功",null) : Result.error(1,"操作失败",null);
    }
}
